package wzw.swing;

import java.awt.Toolkit;
import java.util.HashMap;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextArea;
import javax.swing.text.AttributeSet;
import javax.swing.text.Document;

import wzw.swing.text.PlainBoundedDocument;

/**
 * <p>添加了新功能的JTextField控件。
 * 		1.根据设置的最大长度限定输入长度
 * 		2.根据设置的类型及时判断输入是否合法</p>
 * @author	WUZEWEN on 2006-4-5
 * @version	1.0
 * @see		Object#equals(java.lang.Object)
 * @see		Object#hashCode()
 * @see		HashMap
 * @since	JDK1.4
 */

public class BoundedTextArea extends JTextArea implements PlainBoundedDocument.InsertErrorListener{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	public static int TYPE_ANY_CHAR=0;			//任意字符
	public static int TYPE_LETTER_DIGIT=1;		//字母，数字
	public static int TYPE_LETTER=2;			//字母
	public static int TYPE_INTEGER=3;			//整数
	public static int TYPE_DECIMAL=4;			//小数

	/**
	 * <p>对于BoundedJTextArea的构造函数,因为JTextField有setText(),setColumns()方法，
	 * 		故不再在构造函数中包含该域的初始。只包含体现BoundedJTextArea特征的域初始。</p>
	 * 
	 * @param text 显示的内容
	 * @param maxLength 最大长度
	 * @param textType 类型，纯字符、纯数字、字符数字混合无限制	 *
	 */
	public BoundedTextArea(String text, int textType, int maxLength) {
		//祖先的初始有Setter,故在这里构造函数主要初始自己特征的属性
		super(null, null, 0,0);
		
		if (text != null && maxLength == 0) {     
			maxLength = text.length();    }    
		//System.out.println(getDocument().getClass().toString());
		PlainBoundedDocument plainDoc = (PlainBoundedDocument)getDocument();
		plainDoc.setMaxLength(maxLength); 
		plainDoc.setTextType(textType);
		plainDoc.addInsertErrorListener(this);  
	}
	//public BoundedJTextArea(String text, int maxLength) {    
	//	this(text, 0, maxLength);  
	//} 
	//public BoundedJTextArea(int columns, int maxLength) {    
	//	this(null, columns, maxLength);  
	//}

	public BoundedTextArea() {    
		this( 0 );  
	}	
	public BoundedTextArea(int maxLength) {    
		this( 0, maxLength );  
	}
	public BoundedTextArea(int textType, int maxLength) {    
		this(null, textType, maxLength);  
	}

	
	//Getter And Setter for maxLength
	public void setMaxLength(int maxLength) {    
		((PlainBoundedDocument)getDocument()).setMaxLength(maxLength);  
	}	
	public int getMaxLength() {    
		return ((PlainBoundedDocument)getDocument()).getMaxLength();  
	}

	//Getter And Setter for textType
	public void setTextType(int textType) {    
		((PlainBoundedDocument)getDocument()).setTextType(textType);  
	} 	
	public int getTextType() {    
		return ((PlainBoundedDocument)getDocument()).getTextType();  
	}

	// Override to handle insertion error
	public void insertFailed(PlainBoundedDocument doc, int offset, String str, AttributeSet a,String errInfo) {
		// By default, just beep    
		Toolkit.getDefaultToolkit().beep();  
		JOptionPane.showMessageDialog( this.getParent()==null?this:this.getParent(), errInfo);
	}
	
	/**
	 * Override parent Method,to create default model
	 */	
	protected Document createDefaultModel() {    
		return new PlainBoundedDocument();  
	}  
	
	// Test code	
	public static void main(String[] args) {   
		JFrame f = new JFrame("Bounded Text Field Example");   
		BoundedTextArea tf = new BoundedTextArea(1);    
		JLabel l = new JLabel("Type up to 6 characters: ");    
		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);   
		f.getContentPane().add(tf, "East");    
		f.getContentPane().add(l, "West");       
		f.pack();    
		f.setVisible(true);  
	}

}
